﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.OleDb;

using System.Globalization;
using System.Web.UI.WebControls;
using IGIS.BOL;


namespace NegevGas.Extensions.polygons
{
    public partial class PolygonDetails : System.Web.UI.Page
    {
          protected void Page_Load(object sender, System.EventArgs e)
        {
            CultureInfo cultureInfo = new CultureInfo("he-IL");

            //Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
            //Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;

            if (!IsPostBack)
            {
                Response.Cache.SetExpires(DateTime.Now);
                TimeSpan ts = new TimeSpan(0, 0, 0);
                Response.Cache.SetMaxAge(ts);
              ///  hfLayoutID.Value = Page.Request["LayoutID"];
                ViewState.Add("KeverID", Page.Request["KeverID"]);
                ViewState.Add("LayoutID", "");
            }
            else
            {
                Response.Write("<script>var T = 0; T = setTimeout('displaycontact()','2500');</script>");
          //      ViewState["LayoutID"] = hfLayoutID.Value.ToString();
            }


        }

        //======================================================================================================================
        //Fillup Combobox's lists for bounded inner controls.
        //There are 2 types of cmb, one is - the contact list, using the HE case string for filttering
        //                       second are the rest of lists, using the EN case to difer each other
        //======================================================================================================================
        protected void DetailsView1_DataBound(object sender, System.EventArgs e)
        {
            // ERROR: Not supported in C#: OnErrorStatement


            // Hide Rows: BoneMazeva, Raav
            //DetailsView1.Rows[13].Visible = false;
            //DetailsView1.Rows[14].Visible = false;

            //if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
            //{
            //    DataRowView drv = (DataRowView)DetailsView1.DataItem;
                
            //    //Status List
            //    DropDownList ddl = (DropDownList)DetailsView1.FindControl("ddlStatus1");
            //    FillCmb("ddlStatus", ddl);
            //    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(Convert.ToString(drv[20])));

            //    //Yeshovim List
            //    ddl = (DropDownList)DetailsView1.FindControl("ddlYeshov1");
            //    FillCmb("ddlYeshov", ddl);
            //    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(Convert.ToString(drv[6])));

            //    //Contact1 List
            //    ddl = (DropDownList)DetailsView1.FindControl("ddlIshKesher1");
            //    FillCmb("קרוב משפחה", ddl);
            //    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(Convert.ToString(drv[10])));
            //    //Contact2 List
            //    ddl = (DropDownList)DetailsView1.FindControl("ddlBoneMazeva1");
            //    FillCmb("בונה מצבה", ddl);
            //    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(Convert.ToString(drv[11])));
            //    //Contact3 List
            //    ddl = (DropDownList)DetailsView1.FindControl("ddlRav1");
            //    FillCmb("רב", ddl);
            //    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(Convert.ToString(drv[12])));

            //    //Gets Contact1Type1 List
            //    ddl = (DropDownList)DetailsView1.FindControl("Contact1Type1");
            //    FillCmb("ddlContact1Type", ddl);
            //    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(Convert.ToString(drv[21])));

            //    //Payement Type List
            //    ddl = (DropDownList)DetailsView1.FindControl("ddlPayment1");
            //    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(Convert.ToString(drv[16])));

            //    if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            //    {
            //    }
            //}
            //else
            //{
            //    Response.Write("<script>try{parent.parent.mapFrame.Refresh();}catch(err){}</script>");
            //}

        }

        private void FillCmb(string CmbName, System.Web.UI.WebControls.DropDownList ddl)
        {
            switch (CmbName)
            {
                case "ddlStatus":
                    ddl.DataSource = Status.GetStatus();
                    break;
                case "ddlYeshov":
                    ddl.DataSource = Yeshovim.GetYeshovim();
                    break;
                case "ddlContact1Type":
                    ddl.DataSource = Contacts.GetContactTypes();
                    break;
                default:
                   // ddl.DataSource = Contacts.GetContacts(CmbName, hfLayoutID.Value);
                    break;
            }
            ddl.DataTextField = "fldDesc";
            ddl.DataValueField = "fldID";
            ddl.DataBind();
            ListItem li = new ListItem("-בחר מרשימה-", "0");
            ddl.Items.Insert(0, li);

        }

        protected void DetailsView1_ItemUpdating(object sender, System.Web.UI.WebControls.DetailsViewUpdateEventArgs e)
        {
            //object arrDt = null;
            //DropDownList ddl = default(DropDownList);
            //TextBox tb = default(TextBox);
            //object fldid = DetailsView1.DataKey[Convert.ToInt32(DetailsView1.DataItemIndex)];

            //Customer objC = new Customer();
            //objC.fldID = fldid;
            //objC.CustomerID = ((TextBox)DetailsView1.Rows[1].Cells(1).Controls(0)).Text;
            //objC.FK_KEVER = ((TextBox)DetailsView1.Rows(2).Cells(1).Controls(0)).Text;
            ////objC.HELKA = CType(DetailsView1.Rows(3).Cells(1).Controls(0), TextBox).Text
            //ddl = (DropDownList)DetailsView1.FindControl("ddlStatus1");
            //objC.Status = int.Parse(ddl.SelectedValue);
            ////objC.Fname = CType(DetailsView1.Rows(4).Cells(1).Controls(1), TextBox).Text
            ////objC.Lname = CType(DetailsView1.Rows(5).Cells(1).Controls(1), TextBox).Text
            //objC.Fname = ((TextBox)DetailsView1.FindControl("txtFName")).Text;
            //objC.Lname = ((TextBox)DetailsView1.FindControl("txtLName")).Text;
            //objC.Address = ((TextBox)DetailsView1.Rows(7).Cells(1).Controls(0)).Text;
            //ddl = (DropDownList)DetailsView1.FindControl("ddlYeshov1");
            //objC.Yeshov = int.Parse(ddl.SelectedValue);
            //objC.Zip = ((TextBox)DetailsView1.Rows(8).Cells(1).Controls(0)).Text;
            //tb = (TextBox)DetailsView1.Rows(9).Cells(1).Controls(0);
            //objC.DtBirth = "";
            //if ((!string.IsNullOrEmpty(tb.Text.Trim())))
            //{
            //    arrDt = tb.Text.Split("/");
            //    if ((Information.UBound(arrDt) > 1))
            //        objC.DtBirth = arrDt(1) + "/" + arrDt(0) + "/" + arrDt(2);
            //}
            //tb = (TextBox)DetailsView1.Rows(10).Cells(1).Controls(0);
            //objC.DtPast = "";
            //if ((!string.IsNullOrEmpty(tb.Text.Trim())))
            //{
            //    arrDt = tb.Text.Split("/");
            //    if ((Information.UBound(arrDt) > 1))
            //        objC.DtPast = arrDt(1) + "/" + arrDt(0) + "/" + arrDt(2);
            //}

            //ddl = (DropDownList)DetailsView1.FindControl("ddlIshKesher1");
            //try
            //{
            //    objC.Contact1 = int.Parse(ddl.SelectedValue);
            //}
            //catch (Exception ex)
            //{
            //}
            //ddl = (DropDownList)DetailsView1.FindControl("Contact1Type1");
            //try
            //{
            //    objC.Contact1Type = int.Parse(ddl.SelectedValue);
            //}
            //catch (Exception ex)
            //{
            //}
            //ddl = (DropDownList)DetailsView1.FindControl("ddlBoneMazeva1");
            //try
            //{
            //    objC.Contact2 = int.Parse(ddl.SelectedValue);
            //}
            //catch (Exception ex)
            //{
            //}
            //ddl = (DropDownList)DetailsView1.FindControl("ddlRav1");
            //try
            //{
            //    objC.Contact3 = int.Parse(ddl.SelectedValue);
            //}
            //catch (Exception ex)
            //{
            //}

            //tb = (TextBox)DetailsView1.FindControl("dtREHISHA2");
            //objC.DtRehisha = "";
            //if ((!string.IsNullOrEmpty(tb.Text.Trim())))
            //{
            //    arrDt = tb.Text.Split("/");
            //    if ((Information.UBound(arrDt) > 1))
            //        objC.DtRehisha = arrDt(0) + "/" + arrDt(1) + "/" + arrDt(2);
            //}
            ////
            //tb = (TextBox)DetailsView1.Rows(16).Cells(1).Controls(0);
            //objC.DtGiluiMazeva = "";
            //if ((!string.IsNullOrEmpty(tb.Text.Trim())))
            //{
            //    arrDt = tb.Text.Split("/");
            //    if ((Information.UBound(arrDt) > 1))
            //        objC.DtGiluiMazeva = arrDt(1) + "/" + arrDt(0) + "/" + arrDt(2);
            //}

            //objC.Price = Convert.ToDouble(((TextBox)DetailsView1.Rows[17].Cells[1].Controls[0]).Text);
            //objC.PaymentType = ((TextBox)DetailsView1.Rows(18).Cells(1).Controls(0)).Text;
            //ddl = (DropDownList)DetailsView1.FindControl("ddlPayment1");
            //objC.Payment = ddl.SelectedItem.Text;
            //objC.Comment = ((TextBox)DetailsView1.Rows(20).Cells(1).Controls(0)).Text;
            ////
            //arrDt = ((TextBox)DetailsView1.Rows(21).Cells(1).Controls(0)).Text.Split("/");
            //if ((Information.UBound(arrDt) > 1))
            //    objC.DtPeraoon = arrDt[1] + "/" + arrDt(0) + "/" + arrDt(2);

            //objC.LayoutID = Convert.ToInt16(hfLayoutID.Value.ToString());

            //if (objC.fldID == -1)
            //{
            //    Customer.Insert(objC);
            //}
            //else
            //{
            //    Customer.Update(objC);
            //}

            //DetailsView2.DataBind();
        }

        public void AddNewCustomer(object sender, System.EventArgs e)
        {
            Customer objC = new Customer();
            objC.CustomerID = 0;
            objC.FK_KEVER = Convert.ToInt16(ViewState["KeverID"]);
            objC.Status = "0";
            objC.Fname = "";
            objC.Lname = "";
            objC.Address = "";
            objC.Yeshov = 0;
            objC.Zip = 0;
            objC.Contact1 = 0;
            objC.Contact1Type = 0;
            objC.Contact2 = 0;
            objC.Contact3 = 0;
            objC.Price = 0;
            objC.PaymentType = "";
            objC.Payment = "";
            objC.Comment = "";
           // objC.LayoutID = Convert.ToInt16(hfLayoutID.Value.ToString());


            object rtn = Customer.Insert(objC);
            Response.Redirect("/hetzit/BetOlam/DtlKevarim.aspx?LayoutID=" + ViewState["LayoutID"] + "&KeverID=" + ViewState["KeverID"]);
        }

        public void DetailsView2_DataBound(object sender, System.EventArgs e)
        {
            //DetailsView1.DataBind()
        }

        public void DetailsView2_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
         
        }


        public void AddNewContact(object sender, System.EventArgs e)
        {
            Contacts.InsertContactWithKeverID(Convert.ToInt16(ViewState["LayoutID"]),Convert.ToInt16(ViewState["KeverID"]), "קרוב משפחה");
            //Dim ddl As New DropDownList
            //ddl = CType(DetailsView1.FindControl("ddlIshKesher1"), DropDownList)
            //FillCmb("קרוב משפחה", ddl)

        //    DetailsView1.DataBind();
           
        }
    }

}